

function getSocket() 
{
    var socketOriginal
    var socket = {}
    //统一emit 参数body采用json格式的字符串(window.io是可以直接传obj的)
    if ( typeof(SocketIO) == "undefined" )
    {
        socketOriginal = window.io.connect("game1715.com:8888")
        //这里不填的话会默认本地
        socket.on = function(action, fun)
        {
            socketOriginal.on(action, fun)
        }  
    }
    else 
    {
        socketOriginal = SocketIO.connect('ws://game1715.com:8888', {"force new connection" : true})
        socket.on = function(action, fun)
        {
            socketOriginal.on(action, function(body)
                    {   
                        if(body)
                            body = eval("("+body+")")
                        fun(body)
                    }
                )
        } 
    }
    socket.emit = function(action, body)
    {   
        body = body || "" 
        socketOriginal.emit(action, JSON.stringify(body))
    }

    return socket
}

var ioSocket = 
{   
    connect:function(onConnect)
    {   
        ioSocket.socket = getSocket()

        ioSocket.socket.on('connect', 
        function() 
        {
            cc.log('connect success')
            if (onConnect) onConnect()
            _registerRespHandlers()     
        })
    },
    req:function(action,body)
    {      
        ioSocket.socket.emit(action,body)
    },
 
}

_registerRespHandlers = function() 
{   
    var socket = ioSocket.socket

    socket.on('enterMainScene', 
    function(respData) 
    {   
        cc.log('enterMainScene',respData)
        USERDATA = respData.body
        
        dblMainScene.runThisScene()

    })

    socket.on('appendRoomsList', 
    function(respData) 
    {   
        cc.log('appendRoomsList',respData)        
        dblMainScene.appendRoomsList(respData.body)
    })
    

    socket.on('showRoom', 
    function(respData) 
    {
        cc.log('showRoom',respData)
        ROOMDATA = localizeRoomData(respData.body)
        dblRoomLayer.showRoom()
    })

    socket.on('hideRoom', 
    function(respData) 
    {
        cc.log('hideRoom',respData)
        USERDATA = respData.body
        dblRoomLayer.hideRoom()
        dblMainScene.refreshMain()
    })

    socket.on('refreshRoom', 
    function(respData) 
    {
        cc.log('refreshRoom',respData)
        ROOMDATA = localizeRoomData(respData.body)
        dblRoomLayer.refreshRoom()
    })

    socket.on('refreshCountdown', 
    function(respData) 
    {
        cc.log('refreshCountdown',respData)
        ROOMDATA.countdownSeconds =  respData.body
        dblRoomLayer.refreshCountdown()
    })

    socket.on('dealCards', 
    function(respData) 
    {
        cc.log('dealCards1',respData)
        ROOMDATA = localizeRoomData(respData.body)
        dblRoomLayer.dealCards()
    })

    socket.on('showGameResult', 
    function(respData) 
    {
        cc.log('showGameResult',respData)
        dblRoomLayer.showGameResult(respData.body)
    })

    socket.on('tips', 
    function(respData) 
    {
        cc.log('tips',respData)
        ShowTipsTTF({str:respData.ret})
    })

    socket.on('error', 
    function(err) 
    {
        cc.log('!fail to connect')
        var event = new cc.EventCustom("error")
        cc.eventManager.dispatchEvent(event)
    })

    socket.on('message', 
    function(data)
    { 
        cc.log('socketMsg:',data) 
    })  

}



